class BooksController < ApplicationController
  # GET /books
  # GET /books.xml
  def index
    @books = Book.all
  end

  def get_by_uid
    brs = BorrowRec.find(:all, :conditions => "user_id = #{params[:id]} and status = 'confirmed'")
    @books = []
    if brs.count > 0
      brs.each {|b| @books.push Book.find(b.book_id)}
    end
    render 'index'
  end

  def search
    if request.post?
      isbn = params[:isbn]
      @books = Book.find_all_by_ISBN(isbn)
      render "index"
      return
    end
  end

  # GET /books/1
  # GET /books/1.xml
  def show
    @book = Book.find(params[:id])
  end

  # GET /books/new
  # GET /books/new.xml
  def new
    @book = Book.new
  end

  # GET /books/1/edit
  def edit
    @book = Book.find(params[:id])
  end

  # POST /books
  # POST /books.xml
  def create
    @book = Book.new(params[:book])

    if @book.save
      flash[:notice] = 'Book was successfully created.'
      redirect_to(@book)
    else
      render :action => "new"
    end
  end

  # PUT /books/1
  # PUT /books/1.xml
  def update
    @book = Book.find(params[:id])
    if @book.update_attributes(params[:book])
      flash[:notice] = 'Book was successfully updated.'
      redirect_to(@book)
    else
      render :action => "edit"
    end
  end

  # DELETE /books/1
  # DELETE /books/1.xml
  def destroy
    @book = Book.find(params[:id])
    @book.destroy
    redirect_to(books_url)
  end

  def create_request
    # create new Borrow_Rec
    new_br = BorrowRec.new(
      :user_id => session[:user_id],
      :book_id => params[:id],
      :start_date => nil,
      :due_date => nil,
      :status => 'requested'
    )
    new_br.save
    # create REQUEST message
    new_message = Message.new(
      :mtype => "REQUEST",
      :mtime => Time.new,
      :handled => false,
      :sender => session[:user_id],
      :receiver => User.find(:first, :conditions => "name = 'lib'").id,
      :borrow_rec_id => new_br.id
    )
    new_message.save
    flash[:notice] = 'New Borrow Record and REQUEST message was created!'
    redirect_to request.referer
  end

  def create_reserve
    # create new Borrow_Rec
    new_br = BorrowRec.new(
      :user_id => session[:user_id],
      :book_id => params[:id],
      :start_date => nil,
      :due_date => nil,
      :status => 'reserved'
    )
    new_br.save
    # create RESERVE message
    new_message = Message.new(
      :mtype => "RESERVE",
      :mtime => Time.new,
      :handled => false,
      :sender => session[:user_id],
      :receiver => User.find(:first, :conditions => "name = 'lib'").id,
      :borrow_rec_id => new_br.id
    )
    new_message.save
    flash[:notice] = 'New Borrow Record and RESERVE message was created!'
    redirect_to request.referer
  end

  def create_renew
    br = BorrowRec.find(:first, :conditions => "user_id = #{session[:user_id]} and book_id = #{params[:id]} and status = 'confirmed'")
    # create RENEW message
    if Message.find(:all, :conditions => "borrow_rec_id = #{br.id} and mtype = 'ACCEPT_RENEW'").count >= 3
      flash[:notice] = 'You already renewed the book for three times!'
      redirect_to request.referer
    elsif Message.find(:all, :conditions => "borrow_rec_id = #{br.id} and mtype = 'NOTIFY'").count > 0
      flash[:notice] = 'This book has already been reserved by someone else!'
      redirect_to request.referer
    else
      new_message = Message.new(
        :mtype => "RENEW",
        :mtime => Time.new,
        :handled => false,
        :sender => session[:user_id],
        :receiver => User.find(:first, :conditions => "name = 'lib'").id,
        :borrow_rec_id => br.id
      )
      new_message.save
      flash[:notice] = 'RENEW message was created!'
      redirect_to request.referer
    end
  end

  def create_return
    br = BorrowRec.find(:first, :conditions => "user_id = #{session[:user_id]} and book_id = #{params[:id]} and status = 'confirmed'")
    br.status = 'returned'
    br.save
    # create RETURN message
    new_message = Message.new(
      :mtype => "RETURN",
      :mtime => Time.new,
      :handled => false,
      :sender => session[:user_id],
      :receiver => User.find(:first, :conditions => "name = 'lib'").id,
      :borrow_rec_id => br.id
    )
    new_message.save

    deliver_m = ''
    br_reserved = BorrowRec.find(:first, :conditions => "book_id = #{params[:id]} and status = 'waiting'")
    if br_reserved
      br_reserved.status = 'delivered'
      br_reserved.save
      deliver_message = Message.new(
        :mtype => "DELIVER",
        :mtime => Time.new,
        :handled => false,
        :sender => User.find(:first, :conditions => "name = 'lib'").id,
        :receiver => br_reserved.user_id,
        :borrow_rec_id => br_reserved.id
      )
      deliver_message.save
      deliver_m = 'and new DELIVER message was sent'
    end
    flash[:notice] = 'RETURN message was created' + deliver_m + '!'
    redirect_to request.referer
  end

end
